package com.study.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.study.view.R;

import androidx.annotation.Nullable;

/**
 * author : xujianhong
 * date   : 2023/2/23
 * desc   : 圆形头像
 * version: 1.0
 */
public class RoundImageView extends ImageView {

    Paint mPaint;
    Bitmap mBitmap;
    Bitmap bitmapMask;
    Bitmap mScaledBitmap;
    int strokeWidth = 3;
    int strokeColor = Color.WHITE;

    public RoundImageView(Context context) {
        this(context,null);
    }

    public RoundImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray typeface = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        strokeWidth = typeface.getDimensionPixelOffset(R.styleable.RoundImageView_stroke_width,strokeWidth);
        strokeColor = typeface.getColor(R.styleable.RoundImageView_storke_color,strokeColor);
        typeface.recycle();

        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mBitmap == null) {
            super.onDraw(canvas);
        } else {
            onDrawCircleView(canvas);
        }

    }

    private void onDrawCircleView(Canvas canvas) {
        int saveLayer = canvas.saveLayer(0, 0, getWidth(), getHeight(), mPaint,Canvas.ALL_SAVE_FLAG);

        //绘制目标图像
        canvas.drawBitmap(mScaledBitmap,0 ,0, mPaint);
        //设置画布重叠模式
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        //绘制遮罩
        canvas.drawBitmap(bitmapMask,0,0,mPaint);
        //清空画笔模式
        mPaint.setXfermode(null);

        canvas.restoreToCount(saveLayer);

        //绘制边框
       ondrawBorder(canvas);
    }

    private void ondrawBorder(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(strokeColor);
        mPaint.setStrokeWidth(strokeWidth);

        canvas.drawOval(strokeWidth/2, strokeWidth/2, getWidth() - strokeWidth/2, getHeight() - strokeWidth/2, mPaint);
    }

    private void createMask() {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        bitmapMask = Bitmap .createBitmap(getWidth(),getHeight(),config);

        Canvas canvas = new Canvas(bitmapMask);
        canvas.drawOval(0,0,getWidth(),getHeight(),mPaint);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        BitmapDrawable bitmapDrawable = (BitmapDrawable) getDrawable();
        mBitmap = bitmapDrawable.getBitmap();
        //缩放图片
        scaleBitmap();
        //绘制遮罩图像
        createMask();
    }

    /**
     * 按照控件大小缩放放大图片
     */
    private void scaleBitmap(){
        if(mScaledBitmap == null && mBitmap != null){
            int bitmapWidth = mBitmap.getWidth();
            int bitmapHeight = mBitmap.getHeight();

            int viewWidth = getWidth();
            int viewHeight = getHeight();

            //计算缩放的比例
            float scaleW = viewWidth / (float)bitmapWidth;
            float scaleH = viewHeight / (float)bitmapHeight;

            //用来缩放的矩阵
            Matrix matrix = new Matrix();
            matrix.reset();
            matrix.setScale(scaleW,scaleH);
            mScaledBitmap = Bitmap.createBitmap(mBitmap,0,0,bitmapWidth,bitmapHeight,matrix,true);
        }
    }
}
